<?php

namespace app\controllers;

use app\models\CustomerUpdateLog;
use yii;
use yii\web\Controller;
use app\models\Proxy_company;
use app\models\Staff;
use app\models\Customer;

class ApiController extends Controller
{
    public $layout = false;
    public $enableCsrfValidation = false;

    //收入来源
    public function actionIncome_type()
    {
        $db = new \yii\db\Query();
        $list = $db->select("*")->from(yii::$app->db->tablePrefix . "income_type")->all();
        return json_encode($list);
    }

    public function actionTemplate_action_list()
    {
        return $this->render('template_action_list');
    }

    public function actionSearch_customer()
    {
        $name = yii::$app->request->post('name');
        if ($name) {
            $model = new Customer();
            $info = $model->search_num_by_name($name);
            if ($info) {
                echo json_encode(['code' => 1, 'data' => $info]);
            } else {
                echo json_encode(['code' => 0, 'data' => ""]);
            }
        }
    }

    //模糊查询客户
    public function actionSearch_customer_by_like()
    {
        $name = yii::$app->request->post('name');
        if ($name) {
            $model = new Customer();
            $info = $model->search_customer_by_name(trim($name));
            foreach ($info as $k => $v) {
                $info[$k]['value'] = $v['customer_name'];
            }
            if ($info) {
                echo json_encode(['code' => 1, 'data' => $info]);
            } else {
                echo json_encode(['code' => 0, 'data' => '']);
            }
        } else {
            echo json_encode(['code' => 0, 'data' => '']);
        }
    }

    public function actionAdd_product()
    {
        return $this->render('template_add_product');
    }

    /**
     * render添加仓库费页面
     * @author lzp
     */
    public function actionAdd_repo()
    {
        return $this->render('add_repo');
    }

    /**
     * 添加内陆费页面，
     */
    public function actionAdd_inland()
    {
        $model = new Proxy_company();
        $list = $model->get_proxy_company();
        yii::$app->view->params['list'] = $list;
        return $this->render('add_inland');
    }

    /**
     * 收入拆分页面
     */
    public function actionSplit_income()
    {
        $id = yii::$app->request->get('id');
        yii::$app->view->params['id'] = $id;
        return $this->render('split_income');
    }

    /**客户更新审核界面
     * @return bool|string
     */
    public function actionCustomerUpdate()
    {
        $id = yii::$app->request->get('id');
        if (!is_numeric($id) || $id <= 0) {
            return false;
        }
        $model = new CustomerUpdateLog();
        $list = $model->list(array($id));
        $saleman_model = new Staff();
        $saleman_list = $saleman_model->get_all_saleman();
        $model = new Customer();
        //客户来源
        $refer_list = $model->get_all_customer_refer();

        yii::$app->view->params['customerRefer'] = $refer_list;
        yii::$app->view->params['staff'] = $saleman_list;
        yii::$app->view->params['list'] = $list;
        return $this->render('customerUpdate');
    }

    public function actionCustomerUpdateAct()
    {
        if (yii::$app->request->isPost) {
            $id = yii::$app->request->post('id');
            if (!$id) {
                return false;
            }
            $model = new CustomerUpdateLog();
            $re = $model->getRecordById($id);
            if ($re) {
                $data = json_decode($re['sql'], true);
                $data['follow_man'] = json_encode($data['follow_man']);
                unset($data['orderbynum']);
                $id = $data['id'];
                unset($data['id']);
                $customerModel = new Customer();
                $res = $customerModel->updateRecord($data, $id);
                if ($res) {
                    $model->updateStatus($re['customer_id']);
                    echo json_encode(['code' => 1, 'msg' => '修改成功']);
                } else {
                    echo json_encode(['code' => 0, 'msg' => '修改失败，请重试']);
                }

            }
        }
    }

    public function actionSplitExpress()
    {
        $id = yii::$app->request->get('id');
        if (is_numeric($id)) {
            yii::$app->view->params['id'] = $id;
            return $this->render('splitexpress');
        }

    }

}
